package com.example.mybatis.mapper;


import com.example.mybatis.service.dto.BrandInfo;
import com.example.mybatis.service.dto.RougeInfo;
import com.example.mybatis.service.dto.ShopInfo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author : zhayh
 * @date : 2021-3-31 20:11
 * @description :
 */

@Repository
public interface RougeMapper {
    @Select("select * from brand_info")
    List<BrandInfo> getAllBrandInfo();

    @Select("select * from brand_info where id=#{id}")
    BrandInfo getBrandById(Long id);

    @Select("select * from shop_info where id=#{id}")
    ShopInfo getShopById(Long id);

    @Select("select * from rouge_info where id=#{id}")
    @Results({
            @Result(column = "shop_id", property = "shopInfo",
                    one = @One(select = "getShopById", fetchType = FetchType.EAGER)),
            @Result(column = "brand_id", property = "brandInfo",
                    one = @One(select = "getBrandById", fetchType = FetchType.EAGER))
    })
    RougeInfo selectById(Long id);

    @Insert("insert into rouge_info(rouge_name,rouge_price,shop_id,brand_id) " +
            "values(#{rougeName},#{rougePrice},#{shopInfo.id}, #{brandInfo.id})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
        // 自增Id回传
//    @SelectKey(statement = "select last_insert_id()", keyProperty = "id", keyColumn = "id",
//            resultType = Long.class, before = false)
    Integer insert(RougeInfo rougeInfo);

    @Update("update rouge_info set rouge_price=#{price} where id=#{id}")
    Integer updatePrice(Long id, Float price);

    @Delete("delete from rouge_info where id=#{id}")
    Integer delete(Long id);

    @Select("select count(1) from rouge_info")
    Long getTotalCount();

    @Insert({"insert into brand_info(brand_name, brand_founder, brand_description, brand_country)",
            "<foreach collection='list' item='brandInfo' index='index', separator=','> ",
            "(#{brandName}, #{brandFounder}, #{brandDescription}, #{brandCountry}) ",
            "</foreach> "
    })
    int[] batchInsert(List<BrandInfo> brandInfos);

    @Select({"select a.* from rouge_info a ",
            " left join brand_info b on b.id=a.brand_id ",
            " left join shop_info c on c.id=a.shop_id ",
            " where (a.rouge_name like concat('%',#{keyword},'%') or ",
            " b.brand_name like concat('%',#{keyword},'%') or ",
            " c.shop_name like concat('%',#{keyword},'%')) "
    })
    @Results({
            @Result(column = "shop_id", property = "shopInfo",
                    one = @One(select = "getShopById", fetchType = FetchType.EAGER)),
            @Result(column = "brand_id", property = "brandInfo",
                    one = @One(select = "getBrandById", fetchType = FetchType.EAGER))
    })
    List<RougeInfo> selectWithRowBounds(String keyword, RowBounds rowBounds);

    @Select({"select a.* from rouge_info a ",
            " left join brand_info b on b.id=a.brand_id ",
            " left join shop_info c on c.id=a.shop_id ",
            " where (a.rouge_name like concat('%',#{keyword},'%') or ",
            " b.brand_name like concat('%',#{keyword},'%') or ",
            " c.shop_name like concat('%',#{keyword},'%')) "
    })
    @Results({
            @Result(column = "shop_id", property = "shopInfo",
                    one = @One(select = "getShopById", fetchType = FetchType.EAGER)),
            @Result(column = "brand_id", property = "brandInfo",
                    one = @One(select = "getBrandById", fetchType = FetchType.EAGER))
    })
    List<RougeInfo> selectWithParam(@Param("keyword") String keyword,
                                    @Param("pageNum") int pageNum,
                                    @Param("pageSize") int pageSize);

    @Select({"select a.* from rouge_info a ",
            " left join brand_info b on b.id=a.brand_id ",
            " left join shop_info c on c.id=a.shop_id ",
            " where (a.rouge_name like concat('%',#{keyword},'%') or ",
            " b.brand_name like concat('%',#{keyword},'%') or ",
            " c.shop_name like concat('%',#{keyword},'%')) "
    })
    @Results({
            @Result(column = "shop_id", property = "shopInfo",
                    one = @One(select = "getShopById", fetchType = FetchType.EAGER)),
            @Result(column = "brand_id", property = "brandInfo",
                    one = @One(select = "getBrandById", fetchType = FetchType.EAGER))
    })
    List<RougeInfo> selectByKeyword(@Param("keyword") String keyword);

    @Update("update rouge_info set rouge_name=#{price} where id=#{id}")
    Integer updateName(Long id, String name);
}
